/*
*权限
* **/

import 'package:ifengtool/ifengtool.dart';

enum IfengShareDataType {web,video,image,music}

enum IfengShareCardType { normal,tianxiashiComment}

enum IfengPremissionType { camera, storage, location, voice }

enum IfengVoteState { success, fail, voted}

extension IfengShareDataTypeExtension on IfengShareDataType {
  String get displayTitle {
    switch (this) {
      case IfengShareDataType.video:
        return "video";
      case IfengShareDataType.image:
        return "image";
      case IfengShareDataType.music:
        return "music";
      default:
        return "weweb";
    }
  }
}


extension IfengShareCardTypeExtension on IfengShareCardType {
  String get displayTitle {
    switch (this) {
      case IfengShareCardType.tianxiashiComment:
        return "tianxiashiComment";
      default:
        return "";
    }
  }
}

extension IfengVoteStateExtension on IfengVoteState {
  static IfengVoteState getStateValue(int index) {
    switch (index) {
      case 0:
        return IfengVoteState.fail;
      case 1:
        return IfengVoteState.success;
      case 2:
        return IfengVoteState.voted;
      default:
        throw new FormatException("enum is invalid");
    }
  }
}

extension IfengPremissionTypeExtension on IfengPremissionType {
   String get displayTitle {
     switch (this) {
       case IfengPremissionType.camera:
         return "camera";
       case IfengPremissionType.storage:
         return "storage";
       case IfengPremissionType.location:
         return "location";
       case IfengPremissionType.voice:
         return "voice";
       default:{
         throw new FormatException("enum is invalid");
       }
     }
  }
}


/*
*日夜间
* **/
enum IfengThemeMode { day, dark}

extension IfengThemeModeExtension on IfengThemeMode {
  static IfengThemeMode modeFromStr(String modeStr) {
    if(modeStr == 'dark') {
      return IfengThemeMode.dark;
    }else {
      return IfengThemeMode.day;
    }
  }
}

class IfengDeviceInfo {
  String gv = '';
  String av = '';
  String os = '';
  String deviceid = '';
  String proid = '';
  String uid = '';
  String vt = '';
  String screen = '';
  String publishid = '';
  String df = '';
  IfengThemeMode mode = IfengThemeMode.day;
  double fontScale = 1.0;
  String? jl;//jl
  IfengDeviceInfo();
  IfengDeviceInfo.fromMap(Map data) {
    gv = data["gv"];
    av = data['av'];
    os = data['os'];
    deviceid = data['deviceid'];
    proid = data['proid'];
    uid = data['uid'];
    vt = data['vt'];
    screen = data['screen'];
    publishid = data['publishid'];
    df = data['df'];
    mode = IfengThemeModeExtension.modeFromStr(data['mode']);
    fontScale = data['font'];
    jl = data['jl'];
  }
  IfengDeviceInfo.fromTestData(){
    gv = '7.45.0';
    av = '0';
    os = 'ios_14.2';
    deviceid = 'testdeviceid';
    proid = 'ifengnews';
    uid = 'testdeviceid';
    vt = '5';
    screen = '1242x2208';
    publishid = '4002';
    df = 'iPhone9_2,iphone';
    mode = IfengThemeMode.day;
    fontScale = 1.0;
  }
}

//rnum,ref,showtype,tag
class IfengPageInfo {
  String rnum = '';
  String ref = '';
  String showtype = '';
  String tag = '';
  IfengPageInfo();
  IfengPageInfo.fromMap(Map data) {
    rnum = data['rnum'];
    ref = data["ref"];
    showtype = data['showtype'];
    tag = data['tag'];
  }
  IfengPageInfo.fromTestData(){
    rnum = 't30_0';
    tag = 't30';
    showtype = '';
    ref = 'testref';
  }
}

class IfengRightInfo {
  bool camera = false;
  bool storage = false;
  bool voice = false;
  bool locaton = false;
  IfengRightInfo();
  IfengRightInfo.fromMap(Map data) {
    camera = data['camera'] == 'YES';
    storage = data['storage'] == 'YES';
    voice = data['voice'] == 'YES';
    locaton = data['location'] == 'YES';
  }
}

class IfengUserInfo {
  String guid = '';
  String token = '';
  String nickname = '';
  String userimage = '';
  String userPhone = '';
  String level = '';
  IfengUserInfo();
  IfengUserInfo.fromMap(Map data) {
    guid = data['guid'] ?? '';
    token = data['token'] ?? '';
    nickname = data['nickname'] ?? '';
    userimage = data['userimage'] ?? '';
    userPhone = data['userPhone'] ?? '';
    level = data['level'] ?? '';
  }
  IfengUserInfo.fromTestData(){
    guid = '4000000087996620625';
    token = 'E48CAEEEB9A03D0C71641C55ED906CEE';
    nickname = 'testUser';
    userimage = 'https://x0.ifengimg.com/cmpp/2020/1201/fbd14ebbf8f24d3size27_w360_h360.jpg';
    userPhone = '13021022895';
    level = '9';
  }
}

class IfengShareBean {
  //必传参数
  String shareurl = '';
  String desc = '';
  String title = '';
  String thumbnail = '';
  String documentId = '';
  String wordCode = '';
  //选传参数
  String? type;
  String? refCh;
  String? src;
  String? tag;
  bool? isShareFriends;
  String? roomid;
  IfengShareCardType cardType;
  //webcard
  IfengShareDataType shareDataType;//分享类型 默认web  music  video image
  String? imageBase64; //分享的图片json
  String? weiboContent;//微博分享内容
  String? rToken;
  List<String>? weiboSourceList;//微博分享图片

  //评论分享字段 卡片分享
  String? uname;//自媒体名称
  String? comment_contents; //评论内容

  IfengShareBean.baseBean(this.shareurl,this.title,this.desc,this.thumbnail,this.documentId,this.wordCode,
      {this.type,this.refCh,this.src,this.tag,this.isShareFriends,this.roomid,
        this.cardType = IfengShareCardType.normal,this.imageBase64,this.weiboContent,
        this.uname,this.comment_contents,
        this.weiboSourceList,this.rToken,this.shareDataType = IfengShareDataType.web});

  Map<String,dynamic> toMap() {
    Map<String,dynamic> map = Map<String,dynamic>();
    map['shareurl'] = shareurl;
    map['desc'] = desc;
    map['title'] = title;
    map['thumbnail'] = thumbnail;
    map['documentId'] = documentId;
    map['wordCode'] = wordCode;
    map['shareDataType'] = shareDataType.displayTitle;
    if(type != null) map['type'] = type;
    if(tag != null) map['tag'] = tag;
    if(isShareFriends != null) map['isShareFriends'] = isShareFriends;
    if(roomid != null) map['roomid'] = roomid;
    if(cardType != IfengShareCardType.normal ) map['cardType'] = cardType.displayTitle;
    if(imageBase64 != null) map['imageBase64'] = imageBase64;
    if(refCh != null) map['refCh'] = refCh;
    if(src != null) map['src'] = src;
    if(weiboContent != null) map['weiboContent'] = weiboContent;
    if(weiboSourceList != null && weiboSourceList!.isNotEmpty) map['weiboSourceList'] = weiboSourceList;
    if(uname != null) map['uname'] = uname;
    if(comment_contents != null) map['comment_contents'] = comment_contents;
    if(rToken != null ) map['rToken'] = rToken;
    return map;
  }
}

class IfengLocation {
  String? error;
  double longitude = 0;
  double latitude = 0;
  String name = '';
  String lastCity = '';
  String lastState = '';
  String country = '';
  IfengLocation();
  IfengLocation.fromMap(Map data) {
    this.error = data['error'];
    if(this.error == null) {
      this.longitude = data['longitude'];
      this.latitude = data['latitude'];
      this.name = data['name'];
      this.lastCity = data['lastCity'];
      this.lastState = data['lastState'];
      this.country = data['country'];
    }
  }
}

/*统计基类*/
abstract class IfengStatBase {
  String get statType;
  Map get statMap;
  void send(){
    Ifengtool.getInstance().stat(this.statType,this.statMap);
  }
}


/*vStat
* **/
class IfengStatV extends IfengStatBase {
  String record_type = 'v';
  String vid = '';
  String pdur = '';
  String vdur = '';
  String? yn;
  String? ref;
  String? tag;
  String? type;
  String? ptype;
  String? pgid;
  String? reftype;
  String? simid;
  String? rToken;
  String? atype;
  String? showtype;
  String? src;
  String? vtype;
  String? reason;
  IfengStatV({
    this.vid = '',
    this.pdur = '',
    this.vdur = '',
    this.yn,
    this.ref,
    this.tag,
    this.type,
    this.ptype,
    this.pgid,
    this.reftype,
    this.simid,
    this.rToken,
    this.atype,
    this.showtype,
    this.src,
    this.vtype,
    this.reason
  });
  @override
  // TODO: implement statType
  String get statType => "vStat";
  @override
  // TODO: implement statMap
  Map get statMap {
    Map map = Map();
    map['record_type'] = record_type;
    map['vid'] = vid;
    map['pdur'] = pdur;
    map['vdur'] = vdur;
    if(yn != null) map['yn'] = yn;
    if(ref != null) map['ref'] = ref;
    if(tag != null) map['tag'] = tag;
    if(type != null) map['type'] = type;
    if(ptype != null) map['ptype'] = type;
    if(pgid != null) map['pgid'] = pgid;
    if(reftype != null) map[reftype] = reftype;
    if(simid != null) map['simid'] = simid;
    if(rToken != null) {
      map['rToken'] = rToken;
      map['rn'] = rToken;
    }
    if(atype != null) map['atype'] = atype;
    if(showtype != null) map['showtype'] = showtype;
    if(src != null) map['src'] = src;
    if(vtype != null) map['vtype'] = vtype;
    if(reason != null) map['reason'] = reason;
    return map;
  }
}

/*expse
* **/
class IfengStatExpose extends IfengStatBase {
  String? id;
  String? rnum;
  String? reftype;
  String? ch;
  String? src;
  String? showtype;
  String? pagetype;
  Map<String,String>? other;
  IfengStatExpose({
    this.id,
    this.rnum,
    this.reftype,
    this.ch,
    this.src,
    this.showtype,
    this.pagetype,
    this.other
});
  @override
  // TODO: implement statType
  String get statType => "expose";
  @override
  // TODO: implement statMap
  Map get statMap {
    Map map = Map();
    if(id != null) map['id'] = id;
    if(rnum != null) map['rnum'] = rnum;
    if(reftype != null) map['reftype'] = reftype;
    if(ch != null) map['ch'] = ch;
    if(src != null) map['src'] = src;
    if(showtype != null) map['showtype'] = showtype;
    if(pagetype != null) map['pagetype'] = pagetype;
    if(other != null && other!.isNotEmpty) {
      map.addAll(other!);
    }
    return map;
  }
}

/*发送普通的stat统计
***/
class IfengStatRecord extends IfengStatBase {
  Map map;
  String record_type;
  IfengStatRecord(this.record_type,this.map);
  @override
  // TODO: implement statType
  String get statType => "stat";
  @override
  // TODO: implement statMap
  Map get statMap {
    Map rmap = Map();
    rmap['record_type']= record_type;
    if(map.isNotEmpty) {
      rmap.addAll(this.map);
    }
    return rmap;
  }
}

/*comentstat
* **/
class IfengStateComment extends IfengStatBase {
  String record_type = "commentinfo";
  String commentid = '';
  String? ref;
  String? style;
  String? superid;
  String? source;
  IfengStateComment({
    this.commentid = '',
    this.ref,
    this.style,
    this.superid,
    this.source
});
  @override
  // TODO: implement statType
  String get statType => "commentStat";
  @override
  // TODO: implement statMap
  Map get statMap {
    Map map = Map();
    map['record_type'] = record_type;
    map['commentid'] = commentid;
    if(ref != null) map['ref'] = ref;
    if(style != null)map['style'] = style;
    if(superid != null) map['superid'] = superid;
    if(source != null) map['source'] = source;
    return map;
  }
}

class IfengStatPage extends IfengStatBase {
  String record_type = "2";
  String id = '';
  String? ref;
  String? rnum;
  String? tag;
  String? type;
  String? src;
  String? blt;
  String? sw;
  String? reftype;
  String? simid;
  String? rToken;
  String? xtoken;
  String? showtype;
  String? atype;
  String? ptype;
  String? inurl;
  String? kind;
  Map<String,String>? other;
  IfengStatPage({
    this.id = '',
    this.ref,
    this.rnum,
    this.tag,
    this.type,
    this.src,
    this.blt,
    this.sw,
    this.reftype,
    this.simid,
    this.rToken,
    this.xtoken,
    this.showtype,
    this.atype,
    this.ptype,
    this.inurl,
    this.kind,
    this.other
  });
  @override
  // TODO: implement statType
  String get statType => 'pageStat';
  @override
  // TODO: implement statMap
  Map get statMap {
    Map map = Map();
    map['id'] = id;
    map['record_type'] = record_type;
    if(ref != null) map['ch'] = ref;
    if(rnum != null) map['rnum'] = rnum;
    if(tag != null) map['tag'] = tag;
    if(type != null) map['type'] = type;
    if(src != null) map['src'] = src;
    if(blt != null) map['blt'] = blt;
    if(sw != null) map['sw'] = sw;
    if(reftype != null) map['reftype'] = reftype;
    if(simid != null) map['simid'] = simid;
    if(rToken != null) map['recomToken'] = rToken;
    if(xtoken != null) map['xtoken'] = xtoken;
    if(showtype != null) map['showtype'] = showtype;
    if(atype != null) map['atype'] = atype;
    if(ptype != null) map['ptype'] = ptype;
    if(inurl != null) map['inurl'] = inurl;
    if(kind != null) map['kind'] = kind;
    if(other != null && other!.isNotEmpty) {
      map.addAll(other!);
    }
    return map;
  }
}

/*action 统计
* **/
class IfengStatAction extends IfengStatBase {
  String record_type = "action";
  String? id;
  String? type;
  String? yn;
  String? ch;
  String? ref;
  String? sw;
  String? simid;
  String? reason;
  String? rToken;
  String? xtoken;
  String? tag;
  String? pty;
  String? src;
  String? showtype;
  String? reftype;
  String? chty;
  String? rnum;
  String? psnum;
  String? rfnum;
  Map<String,String>? other;
  IfengStatAction({
    this.id,
    this.type,
    this.yn,
    this.ch,
    this.ref,
    this.sw,
    this.simid,
    this.reason,
    this.rToken,
    this.xtoken,
    this.tag,
    this.pty,
    this.src,
    this.showtype,
    this.reftype,
    this.chty,
    this.rnum,
    this.psnum,
    this.rfnum,
    this.other
});
  @override
  // TODO: implement statType
  String get statType => "actionStat";
  @override
  // TODO: implement statMap
  Map get statMap {
    Map map = Map();
    map["record_type"] = record_type;
    if(id != null) map['id'] = id;
    if(type != null) map['type'] = type;
    if(yn != null) map['yn'] = yn;
    if(ch != null) map['ch'] = ch;
    if(ref != null) map['ref'] = ref;
    if(sw != null) map['sw'] = sw;
    if(simid != null) map[simid] = simid;
    if(reason != null) map['reason'] = reason;
    if(rToken != null) map['rToken'] = rToken;
    if(xtoken != null) map['xtoken'] = xtoken;
    if(tag != null) map['tag'] = tag;
    if(pty != null) map['pty'] = pty;
    if(src != null) map['src'] = src;
    if(showtype != null) map['showtype'] = showtype;
    if(reftype != null) map['reftype'] = reftype;
    if(chty != null) map['chty'] = chty;
    if(rnum != null) map['rnum'] = rnum;
    if(psnum != null) map['psnum'] = psnum;
    if(rfnum != null) map['rfnum'] = rfnum;
    if(other != null && other!.isNotEmpty) {
      map.addAll(other!);
    }
    return map;
  }
}

class IfengSubscribe {
  String followId = '';
  bool state = false;
  IfengSubscribe.fromMap(Map data) {
   this.followId = data['followId'];
   this.state = data['state'];
  }
}

class IfengWebMethod {
  String methodKey;
  Map map;
  IfengWebMethod(this.methodKey,this.map);
}

class IfengNetProxy {
  String host;
  int port;
  IfengNetProxy(this.host,this.port);
}

class IfengVoteResultQuestion {
  String questionId;
  List<String> optionIds;
  IfengVoteResultQuestion(this.questionId,this.optionIds);
}

/*struct
{
isVoted:bool 是否投过票,
voteId:String 投票id
questions:[
    {questionId:questionId1,optionIds:[optionId1,optionId2]},
    {questionId:questionId1,optionIds:[optionId1,optionId2]},
    ] 问题数组
}
* **/
class IfengVoteResult {
  bool isVoted = true;
  String voteId = '';
  List<IfengVoteResultQuestion>? questions;
  IfengVoteResult.fromMap(Map map){
    if(map['isVoted'] != null) this.isVoted = map['isVoted'];
    this.voteId = map['voteId'];
    if(this.isVoted && map['questions'] != null) {
      //投票了 才有questions
      List list = map['questions'].cast<Map>();
      if(list.isNotEmpty) {
        this.questions = list.map((e)=>IfengVoteResultQuestion(e['questionId'], e['optionIds'].cast<String>())).toList();
      }
    }
  }
  Map toMap() {
    Map map = Map();
    map['voteId'] = this.voteId;
    if(this.questions != null && this.questions!.isNotEmpty) {
      map['questions'] = this.questions!.map((e) => {'questionId':e.questionId,'optionIds':e.optionIds}).toList();
    }
    return map;
  }
}

class IfengLocalMedia {
  String? id;
  int? width;
  int? height;
  int? size;
  int? duration;
  String? path;
  String? poster;
  IfengLocalMedia({
    this.id,
    this.width,
    this.height,
    this.size,
    this.duration,
    this.path,
    this.poster,
  });
  IfengLocalMedia.fromMap(Map data) {
    this.id = data['id'];
    this.width = data['width'];
    this.height = data['height'];
    this.size = data['size'];
    this.duration = data['duration'];
    this.path = data['path'];
    this.poster = data['poster'];
  }
}

class IfengHistoryBean {
  String id;
  String title;
  String type;
  String url;
  String? thumbnail;
  int videoLength = 0;
  int slide_count = 0;
  String? queryString;
  String? userId;
  String? userName;
  String? userAvatar;

  IfengHistoryBean(this.id,this.title,this.type,this.url,{this.thumbnail,this.videoLength = 0,this.slide_count = 0,this.queryString,this.userId,this.userName,this.userAvatar});
  Map toMap() {
    Map map = Map();
    map['id'] = this.id;
    map['title'] = this.title;
    map['type'] = this.type;
    map['url'] = this.url;
    map['videoLength'] = videoLength;
    map['slide_count'] = slide_count;
    if(queryString != null) map['queryString'] = queryString;
    if(thumbnail != null) map['thumbnail'] = thumbnail;
    if(userId != null) map['userId'] = userId;
    if(userName != null) map['userName'] = userName;
    if(userAvatar != null) map['userAvatar'] = userAvatar;
    return map;
  }
}



